package com.transaction.stockseata.config;

import com.transaction.stockseata.filter.RepeatableReadJsonFilter;
import com.transaction.stockseata.interceptor.IdempotenceInterceptor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

/**
 * @author ausu
 * @version 1.0
 * @description: TODO
 * @date 2023/1/30 14:08
 */
@Configuration
public class WebConfig implements WebMvcConfigurer {

    @Autowired
    IdempotenceInterceptor idempotenceInterceptor;

    /**
     * Add Spring MVC lifecycle interceptors for pre- and post-processing of
     * controller method invocations and resource handler requests.
     * Interceptors can be registered to apply to all requests or be limited
     * to a subset of URL patterns.
     *
     * @param registry
     */
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(idempotenceInterceptor);
    }

    @Bean
    public FilterRegistrationBean<RepeatableReadJsonFilter> repeatableFilterBean() {
        FilterRegistrationBean<RepeatableReadJsonFilter> bean = new FilterRegistrationBean<>();
        bean.addUrlPatterns("/**");
        bean.setFilter(new RepeatableReadJsonFilter());
        return bean;
    }
}
